home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / DCLAP 6d / dclap6d / corelib / ncbimain.mac < prev    next >
Text File  |  1996-07-05  |  9KB  |  334 lines

  1. /*   ncbimain.c
  2. * ===========================================================================
  3. *
  4. *                            PUBLIC DOMAIN NOTICE                          
  5. *               National Center for Biotechnology Information
  6. *                                                                          
  7. *  This software/database is a "United States Government Work" under the   
  8. *  terms of the United States Copyright Act.  It was written as part of    
  9. *  the author's official duties as a United States Government employee and 
  10. *  thus cannot be copyrighted.  This software/database is freely available 
  11. *  to the public for use. The National Library of Medicine and the U.S.    
  12. *  Government have not placed any restriction on its use or reproduction.  
  13. *                                                                          
  14. *  Although all reasonable efforts have been taken to ensure the accuracy  
  15. *  and reliability of the software and data, the NLM and the U.S.          
  16. *  Government do not and cannot warrant the performance or results that    
  17. *  may be obtained by using this software or data. The NLM and the U.S.    
  18. *  Government disclaim all warranties, express or implied, including       
  19. *  warranties of performance, merchantability or fitness for any particular
  20. *  purpose.                                                                
  21. *                                                                          
  22. *  Please cite the author in any work or product based on this material.   
  23. *
  24. * ===========================================================================
  25. *
  26. * File Name:  ncbimain.c
  27. *
  28. * Author:  Ostell
  29. *
  30. * Version Creation Date:   6/4/91
  31. *
  32. * Version Number:  1.1
  33. *
  34. * File Description: 
  35. *       portable main() and argc, argv handling
  36. *       Macintosh version for use with console functions
  37. *
  38. * Modifications:  
  39. * --------------------------------------------------------------------------
  40. * Date     Name        Description of modification
  41. * -------  ----------  -----------------------------------------------------
  42. * 6/4/91   Kans        GetArgs uses ccommand from <console.h>
  43. *
  44. * ==========================================================================
  45. */
  46.  
  47. #include <ncbi.h>
  48. #include <ncbiwin.h>
  49.  
  50. #ifdef COMP_THINKC
  51. #include <console.h>
  52. #endif
  53.  
  54. #ifdef COMP_MPW
  55. #pragma segment NlmSegA
  56. #endif
  57.  
  58. /*****************************************************************************
  59. *
  60. *   main()
  61. *     this replaces the normal program main()
  62. *     does any required windows initialization
  63. *
  64. *****************************************************************************/
  65. static int targc;
  66. static char ** targv;
  67.  
  68. #ifdef COMP_THINKC
  69. main (int argc, char ** argv)
  70. #endif
  71. #ifdef COMP_MPW
  72. main (int argc, char *argv[])
  73. #endif
  74.  
  75. {
  76.     Nlm_Int2 retval;
  77.  
  78.     targc = argc;
  79.     targv = argv;
  80.                        /**** do any initialization here ******/
  81.     retval = Nlm_Main();
  82.                        /**** do any cleanup here *************/
  83.     Nlm_FreeConfigStruct ();
  84. /*
  85.     ExitToShell();
  86. */
  87. }
  88. #define MAX_ARGS 50       /* maximum args this can process */
  89. /*****************************************************************************
  90. *
  91. *   Nlm_GetArgs(ap)
  92. *       returns user startup arguments
  93. *
  94. *****************************************************************************/
  95. Nlm_Boolean Nlm_GetArgs (Nlm_CharPtr progname, Nlm_Int2 numargs, Nlm_ArgPtr ap)
  96.  
  97. {
  98.     static Nlm_CharPtr types[9] = {
  99.         NULL,
  100.         "T/F",
  101.         "Integer",
  102.         "Real",
  103.         "String",
  104.         "File In",
  105.         "File Out",
  106.         "Data In",
  107.         "Data Out" };
  108.     Nlm_Boolean okay = FALSE, all_default = TRUE, range;
  109.     Nlm_Int2 i, j;
  110.     Nlm_Int4 ifrom, ito;
  111.     Nlm_FloatHi ffrom, fto;
  112.     Nlm_ArgPtr curarg;
  113.     Nlm_Boolean resolved[MAX_ARGS];
  114.     Nlm_CharPtr arg;
  115.     Nlm_Char tmp;
  116.     Nlm_Boolean ok;
  117.  
  118. #ifdef COMP_THINKC
  119.     targc = ccommand(&targv);
  120. #endif
  121.  
  122.     if ((ap == NULL) || (numargs == 0) || (numargs > MAX_ARGS))
  123.         return okay;
  124.  
  125.     curarg = ap;                        /* set defaults */
  126.     Nlm_MemFill(resolved, '\0', (MAX_ARGS * sizeof(Nlm_Boolean)));
  127.  
  128.     for (i = 0; i < numargs; i++, curarg++)
  129.     {
  130.         if ((curarg->type < ARG_BOOLEAN) ||
  131.              (curarg->type > ARG_DATA_OUT))
  132.         {
  133.             Message(MSG_ERROR, "Invalid Arg->type in %s", curarg->prompt);
  134.             return okay;
  135.         }
  136.         curarg->intvalue = 0;
  137.         curarg->floatvalue = 0.0;
  138.         curarg->strvalue = NULL;
  139.         if (curarg->defaultvalue != NULL)
  140.         {
  141.             resolved[i] = TRUE;
  142.             switch (curarg->type)
  143.             {
  144.                 case ARG_BOOLEAN:
  145.                     if (TO_UPPER(*curarg->defaultvalue) == 'T')
  146.                         curarg->intvalue = 1;
  147.                     else
  148.                         curarg->intvalue = 0;
  149.                     break;
  150.                 case ARG_INT:
  151.                     sscanf(curarg->defaultvalue, "%ld", &curarg->intvalue);
  152.                     break;
  153.                 case ARG_FLOAT:
  154.                     sscanf(curarg->defaultvalue, "%lf", &curarg->floatvalue);
  155.                     break;
  156.                 case ARG_STRING:
  157.                 case ARG_FILE_IN:
  158.                 case ARG_FILE_OUT:
  159.                 case ARG_DATA_IN:
  160.                 case ARG_DATA_OUT:
  161.                     curarg->strvalue = StringSave (curarg->defaultvalue);
  162.                     break;
  163.             }
  164.         }
  165.         else if (curarg->optional == FALSE)
  166.             all_default = FALSE;    /* must have some arguments */
  167.     }
  168.                           /**** show usage if no args on command line ***/
  169.     if ((targc == 1) && (all_default == TRUE))   /* no args ok */
  170.         return TRUE;
  171.  
  172.     if ((targc == 1) || (*(targv[1]+1) == '\0'))
  173.     {
  174.         printf("\n%s   arguments:\n\n", progname);
  175.         curarg = ap;
  176.  
  177.         for (i = 0, j=0; i < numargs; i++, j++, curarg++)
  178.         {
  179.             printf("  -%c  %s [%s]", curarg->tag, curarg->prompt,
  180.                 types[curarg->type]);
  181.             if (curarg->optional)
  182.                 printf("  Optional");
  183.             printf("\n");
  184.             if (curarg->defaultvalue != NULL)
  185.                 printf("    default = %s\n", curarg->defaultvalue);
  186.             if ((curarg->from != NULL) || (curarg->to != NULL))
  187.             {
  188.                 if ((curarg->type == ARG_DATA_IN) ||
  189.                     (curarg->type == ARG_DATA_OUT))
  190.                     printf("    Data Type = %s\n", curarg->from);
  191.                 else
  192.                     printf("    range from %s to %s\n", curarg->from, curarg->to);
  193.             }
  194.         }
  195.         fflush (stdout);
  196. #ifdef COMP_MPW
  197.         Message(MSG_OK, "To exit: ");
  198. #endif
  199.         return okay;
  200.     }
  201.  
  202.     for (i = 1; i < targc; i++)
  203.     {
  204.         arg = targv[i];
  205.         if (*arg != '-')
  206.         {
  207.             Message(MSG_ERROR, "Arguments must start with -");
  208.             return okay;
  209.         }
  210.         arg++;
  211.         curarg = ap;
  212.         for (j = 0; j < numargs; j++, curarg++)
  213.         {
  214.             if (*arg == curarg->tag)
  215.                 break;
  216.         }
  217.         if (j == numargs)
  218.         {
  219.             Message(MSG_ERROR, "Invalid argument: %s", targv[i]);
  220.             return okay;
  221.         }
  222.         arg++;
  223.         arg++;
  224.         if (*arg == '\0')
  225.         {
  226.             ok = FALSE;
  227.             if ((i + 1) < targc)  /* argument comes after space */
  228.             {
  229.                 if (*targv[i + 1] == '-')
  230.                 {
  231.                     tmp = *(targv[i+1]+1);
  232.                     if (((curarg->type == ARG_INT) || (curarg->type == ARG_FLOAT)) &&
  233.                         ((tmp == '.') || (IS_DIGIT(tmp))))
  234.                         ok = TRUE;
  235.                 }
  236.                 else
  237.                     ok = TRUE;
  238.                 if (ok)
  239.                 {
  240.                     i++;
  241.                     arg = targv[i];
  242.                 }
  243.             }
  244.  
  245.             if ((!ok) && (curarg->type != ARG_BOOLEAN))
  246.             {
  247.                 Message(MSG_ERROR, "No argument given for %s", curarg->prompt);
  248.                 return okay;
  249.             }
  250.         }
  251.         resolved[j] = TRUE;
  252.         switch (curarg->type)
  253.         {
  254.             case ARG_BOOLEAN:
  255.                 if (TO_UPPER(*arg) == 'T')
  256.                     curarg->intvalue = 1;
  257.                 else if (TO_UPPER(*arg) == 'F')
  258.                     curarg->intvalue = 0;
  259.                 else if (*arg == '\0')
  260.                     curarg->intvalue = 1;
  261.                 break;
  262.             case ARG_INT:
  263.                 sscanf(arg, "%ld", &curarg->intvalue);
  264.                 if ((curarg->from != NULL) || (curarg->to != NULL))
  265.                 {
  266.                     range = TRUE;
  267.                     if (curarg->from != NULL)
  268.                     {
  269.                         sscanf(curarg->from, "%ld", &ifrom);
  270.                         if (curarg->intvalue < ifrom)
  271.                             range = FALSE;
  272.                     }
  273.                     if (curarg->to != NULL)
  274.                     {
  275.                         sscanf(curarg->to, "%ld", &ito);
  276.                         if (curarg->intvalue > ito)
  277.                             range = FALSE;
  278.                     }
  279.                     if (! range)
  280.                     {
  281.                         Message(MSG_ERROR, "%s [%ld] is out of range [%s to %s]", curarg->prompt,
  282.                             curarg->intvalue, curarg->from, curarg->to);
  283.                         return okay;
  284.                     }
  285.                 }
  286.                  break;
  287.             case ARG_FLOAT:
  288.                 sscanf(arg, "%lf", &curarg->floatvalue);
  289.                 if ((curarg->from != NULL) || (curarg->to != NULL))
  290.                 {
  291.                     range = TRUE;
  292.                     if (curarg->from != NULL)
  293.                     {
  294.                         sscanf(curarg->from, "%lf", &ffrom);
  295.                         if (curarg->floatvalue < ffrom)
  296.                             range = FALSE;
  297.                     }
  298.                     if (curarg->to != NULL)
  299.                     {
  300.                         sscanf(curarg->to, "%lf", &fto);
  301.                         if (curarg->floatvalue > fto)
  302.                             range = FALSE;
  303.                     }
  304.                     if (! range)
  305.                     {
  306.                         Message(MSG_ERROR, "%s [%g] is out of range [%s to %s]", curarg->prompt, 
  307.                             curarg->floatvalue, curarg->from, curarg->to);
  308.                         return okay;
  309.                     }
  310.                 }
  311.                 break;
  312.             case ARG_STRING:
  313.             case ARG_FILE_IN:
  314.             case ARG_FILE_OUT:
  315.             case ARG_DATA_IN:
  316.             case ARG_DATA_OUT:
  317.                 curarg->strvalue = StringSave (arg);
  318.                 break;
  319.         }    /*** end switch ****/
  320.     }       
  321.     curarg = ap;
  322.     okay = TRUE;
  323.     for (i = 0; i < numargs; i++, curarg++)
  324.     {
  325.         if ((! curarg->optional) && (! resolved[i]))
  326.         {
  327.             Message(MSG_ERROR, "%s was not given an argument", curarg->prompt);
  328.             okay = FALSE;
  329.         }
  330.     }
  331.     return okay;
  332. }
  333.  
  334.